//
//  YZPigGroupDeathDetailViewModel.m
//  VET
//
//  Created by YZ on 2018/7/24.
//  Copyright © 2018年 YZ. All rights reserved.
//

#import "YZPigGroupDeathDetailViewModel.h"
#import "YZPigDeathDetailModel.h"
#import "YZPigDeathDetailRecordModel.h"
#import "NSDate+Category.h"
#import "YZSwineryModel.h"

@implementation YZPigGroupDeathDetailViewModel

@synthesize dataSource = _dataSource;
@synthesize index = _index;

- (instancetype)init
{
    if(self = [super init]){
        [self config];
    }
    return self;
}

- (void)config
{
    _dataSource = @[[NSMutableArray array],[NSMutableArray array]];
}


- (void)headerRefreshSuccess:(void (^)(id))successBlock fail:(void (^)(NSError *))failBlock
{
    _index = 0;
    YZWeakSelf(self);
    NSMutableArray *arr = (NSMutableArray *)_dataSource.lastObject;
    [self requestDieOutTrackSuccess:^(NSDictionary *obj) {
        if (((NSArray *)((NSDictionary *)obj[@"data"])[@"list"]).count != 0) {
            weakself.totalPage = [((NSDictionary *)obj[@"data"])[@"lastPage"] integerValue] - 1;
        }else
        {
            weakself.totalPage = 0;
        }
        [arr removeAllObjects];
        if (((NSArray *)((NSDictionary *)obj[@"data"])[@"list"]).count == 0) {
            NSLog(@"adfadf");
        }
        for (NSDictionary *dic in (NSArray *)((NSDictionary *)obj[@"data"])[@"list"]) {
            YZPigDeathDetailRecordModel *model = [YZPigDeathDetailRecordModel mj_objectWithKeyValues:dic];
            [arr addObject:model];
        }
        if (successBlock) {
            successBlock(arr);
        }
       
    } fail:^(NSError *error) {
        if (failBlock) {
            failBlock(error);
        }
    }];
}

- (void)footerRefreshSuccess:(void (^)(id))successBlock fail:(void (^)(NSError *))failBlock
{
    _index++;
    self.loading = YES;
    NSMutableArray *arr = (NSMutableArray *)_dataSource.lastObject;
    [self requestDieOutTrackSuccess:^(NSDictionary *obj) {
        self.loading = NO;
        for (NSDictionary *dic in (NSArray *)((NSDictionary *)obj[@"data"])[@"list"]) {
            YZPigDeathDetailRecordModel *model = [YZPigDeathDetailRecordModel mj_objectWithKeyValues:dic];
            [arr addObject:model];
        }
        if (successBlock) {
            successBlock(((NSDictionary *)obj[@"data"])[@"list"]);
        }
    } fail:^(NSError *error) {
        self.loading = NO;
        if (failBlock) {
            failBlock(error);
        }
    }];
}

- (void)requestDieOutTrackSuccess:(YZNetworkCompletionSuccessBlock)successBlock fail:(YZNetworkCompletionFailBlock)failBlock
{
    NSDictionary *params = @{@"index":[NSString stringWithFormat:@"%ld",_index],@"size":@"10"};
    [YZNetworkManager getWithApi:[NSString stringWithFormat:@"/api/dieout/track/%ld",self.model.dieOutId] params:params withHeaders:nil success:^(NSDictionary *obj) {
        if (successBlock) {
            successBlock(obj);
        }
    } fail:^(NSError *error) {
        if (failBlock) {
            failBlock(error);
        }
    }];
}

- (void)setModel:(YZDieOutModel *)model
{
    _model = model;
 
    YZPigDeathDetailModel *earModel = [YZPigDeathDetailModel new];
    earModel.title = @"批次号";
    earModel.content = model.dieOutNO;
    
    YZPigDeathDetailModel *typeModel = [YZPigDeathDetailModel new];
    typeModel.title = @"分类";
    typeModel.content = model.dieoutClass.itemValue;
    
    YZPigDeathDetailModel *deathNumModel = [YZPigDeathDetailModel new];
    deathNumModel.title = @"死亡头数";
    deathNumModel.content = [NSString stringWithFormat:@"%ld",model.dieoutNum];
    
    YZPigDeathDetailModel *dateModel = [YZPigDeathDetailModel new];
    dateModel.title = @"日龄 (天)";
    dateModel.content = [NSString stringWithFormat:@"%ld",(NSInteger)ceil((model.dieoutTime - model.birthdate)/(24.0*3600))];
    
    YZPigDeathDetailModel *deathModel = [YZPigDeathDetailModel new];
    deathModel.title = @"死亡日期";
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:model.dieoutTime];
    deathModel.content = [date localDateString];
    
    YZPigDeathDetailModel *statusModel = [YZPigDeathDetailModel new];
    statusModel.title = @"死前状态";
    statusModel.content = model.dieoutState.itemValue;
    
    NSMutableArray *arr = (NSMutableArray *)_dataSource.firstObject;
    [arr removeAllObjects];
    [arr addObjectsFromArray:@[earModel,typeModel,deathNumModel,dateModel,deathModel,statusModel]];
}
@end
